const fs = require("fs");
const readline = require('readline');
const path = require('path');
const util = require('util');

//node test\file.js  views 可以 ../views 不行  node file.js    ../views 可以

const fileLines = [];
const filePath = 'views/test.txt';

fs.readFile(filePath, function (err, data) {
    if (err) {
        console.log(err);
        return;
    } 
    console.log(data.toString());
});

let input = fs.createReadStream(filePath);
const line = readline.createInterface({
  input: input
});

line.on('line', (line) => {
  fileLines.push(line);
  console.log(`Line from file: ${line}`);
});
line.on('close', (line) => {
  console.log("读取完毕！");
});

//callback -> promise (err, value)=>{}  util.promisify.custom
const readFile = util.promisify(fs.readFile);
// readFile(filePath).then(data => console.log(data.toString())); 

let cons = readline.createInterface({
  input:process.stdin,
  output:process.stdout
});

cons.on('line', (line) => {
  console.log(`Line from console: ${line}`);
  if (line.includes("exit")) {
    cons.close();
  }
  // cons.write(`hello: ${line}`);
});

cons.on('close', (line) => {
  console.log("结束输出");
  process.exit(0);
});